package com.itheima.health.controller.backend;

import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hm
 * @version 1.0
 * @date Created in 2024/7/13 12:20
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    /**
     * 新增体检检查项
     */
    @Resource
    private CheckItemService checkItemService;
    @Resource
    private CheckGroupService checkGroupService;

    /**
     * 新增体检检查项
     */
    @PostMapping(("/add.do"))
    public Result add(@RequestBody CheckItem checkItem) {
        checkItemService.add(checkItem);
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 分页查询体检检查项
     */
    @PostMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkItemService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 通过id查找体检检查项
     */
    @GetMapping("/findById.do")
    public Result findById(Integer id) {
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemService.findById(id));
    }
    /**
     * 通过id修改体检项检查项
     */
    @PostMapping("/edit.do")
    public Result update(@RequestBody CheckItem checkItem){
        checkItemService.update(checkItem);
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }
    /**
     * 通过id删除体检检查项
     */
    @GetMapping("/delete.do")
    public Result deleteById(Integer id) {
        List<Integer> list = checkItemService.fingCheckgroupId(id);
        if (list.size()!=0){
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }else {
            checkItemService.deleteById(id);
        }
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }
    /**
     * 查询所有得检查项
     */
    @GetMapping("findAll.do")
    public Result findAll(){
        List<CheckItem> checkItems = checkGroupService.findAll();
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItems);
    }
}
